package com.zjj.sc.movie.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjj.sc.movie.controller.UserFeignClient.FeignClientFallbackFactory;
import com.zjj.sc.movie.dto.User;

import feign.hystrix.FallbackFactory;

@FeignClient(name = "microservice-4-provider-user" , fallbackFactory = FeignClientFallbackFactory.class)
public interface UserFeignClient {
	
	@RequestMapping(value = "/{id}" , method = RequestMethod.GET)
	public User findById(@PathVariable("id") Long id);
	

	@Component
	class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {

		private static final Logger logger = LoggerFactory.getLogger(FeignClientFallbackFactory.class);
		
		@Override
		public UserFeignClient create(final Throwable cause) {
			return new UserFeignClient() {
				@Override
				public User findById(Long id) {
					
					FeignClientFallbackFactory.logger.info("fallback , reason is : {}" , cause);
					User user = new User();
					user.setId(-3L);
					user.setName("wwwwwwwwwwww");
					return user;
				}
			};
		}
		
	}

}
